package cn.cttic.order.mapper;

import cn.cttic.order.api.domain.BaseReconciliation;
import cn.cttic.order.api.domain.vo.BaseReconciliationDetailReaderVo;
import cn.cttic.order.api.domain.vo.BaseReconciliationVo;
import cn.cttic.common.mybatis.core.mapper.BaseMapperPlus;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 对账管理Mapper接口
 *
 * @author Times.Studio
 * @date 2025-01-02
 */
public interface BaseReconciliationMapper extends BaseMapperPlus<BaseReconciliation, BaseReconciliationVo> {

    @Select("<script>" +
        "select\n" +
        "  bc.id as company_id,\n" +
        "	bso.last_name as car_owner_name,\n" +
        "	biomp.car_no as car_no,\n" +
        "	concat(bd.device_name, bdi.terminal_model) as terminal_name_model,\n" +
        "	bdi.terminal_imei as terminal_imei,\n" +
        "	bdi.terminal_iccid as terminal_iccid,\n" +
        "	bio.submit_time as install_time,\n" +
        "	bio.active_time as active_time,\n" +
        "	bio.last_online_time as last_online_time,\n" +
        "	timestampdiff(DAY, bio.last_online_time, now()) as offline_day,\n" +
        "	biomp.vehicle_type as vehicle_type,\n" +
        "	bio.verify_status,\n" +
        "	bio.review_status,\n" +
        "	bio.review_reason,\n" +
        "	bio.id as install_order_id,\n" +
        "	bso.id as sale_order_id\n" +
        "from\n" +
        "	base_sale_order bso\n" +
        "inner join base_product bp on (bp.id = bso.product_id and bp.del_flag = 0)\n" +
        "inner join base_device bd on (bd.id = bp.device_id and bd.del_flag = 0)\n" +
        "inner join base_install_order bio on (bio.sale_order_id  = bso.id and bio.del_flag = 0 and bso.del_flag = 0 and bio.review_status = 1)\n" +
        "inner join base_device_info bdi on (bdi.install_order_id = bio.id and bdi.del_flag = 0)\n" +
        "inner join base_install_order_main_page biomp on (biomp.install_order_id = bio.id and biomp.del_flag = 0)\n" +
        "inner join base_install_order_sub_page biosp on (biosp.install_order_id = bio.id and biosp.del_flag = 0)\n" +
        "inner join base_user bu on (bu.id = bso.sales_man_id and bu.del_flag = 0)\n" +
        "inner join base_company bc on (bu.company_id = bc.id and bc.del_flag = 0)\n" +
        "${ew.customSqlSegment}" +
        "</script>")
    List<BaseReconciliationDetailReaderVo> getSaleOrderReconciliationList(@Param(Constants.WRAPPER) QueryWrapper queryWrapper);


    @Select("<script>" +
        "select\n" +
        "  bc.id as company_id,\n" +
        "	bso.last_name as car_owner_name,\n" +
        "	biomp.car_no as car_no,\n" +
        "	concat(bd.device_name, bdi.terminal_model) as terminal_name_model,\n" +
        "	bdi.terminal_imei as terminal_imei,\n" +
        "	bdi.terminal_iccid as terminal_iccid,\n" +
        "	bio.submit_time as install_time,\n" +
        "	bio.active_time as active_time,\n" +
        "	bio.last_online_time as last_online_time,\n" +
        "	timestampdiff(DAY, bio.last_online_time, now()) as offline_day,\n" +
        "	biomp.vehicle_type as vehicle_type,\n" +
        "	bio.verify_status,\n" +
        "	bio.review_status,\n" +
        "	bio.review_reason,\n" +
        "	bio.id as install_order_id,\n" +
        "	bso.id as sale_order_id\n" +
        "from\n" +
        "	base_sale_order bso\n" +
        "inner join base_product bp on (bp.id = bso.product_id and bp.del_flag = 0)\n" +
        "inner join base_device bd on (bd.id = bp.device_id and bd.del_flag = 0)\n" +
        "inner join base_install_order bio on (bio.sale_order_id  = bso.id and bio.del_flag = 0 and bso.del_flag = 0 and bio.review_status = 1)\n" +
        "inner join base_device_info bdi on (bdi.install_order_id = bio.id and bdi.del_flag = 0)\n" +
        "inner join base_install_order_main_page biomp on (biomp.install_order_id = bio.id and biomp.del_flag = 0)\n" +
        "inner join base_install_order_sub_page biosp on (biosp.install_order_id = bio.id and biosp.del_flag = 0)\n" +
        "inner join base_user bu on (bu.id = bio.install_man_id and bu.del_flag = 0)\n" +
        "inner join base_company bc on (bu.company_id = bc.id and bc.del_flag = 0)\n" +
        "${ew.customSqlSegment}" +
        "</script>")
    List<BaseReconciliationDetailReaderVo> getInstallOrderReconciliationList(@Param(Constants.WRAPPER) QueryWrapper queryWrapper);

    @Select("<script>" +
        "SELECT DISTINCT\n" +
        "  bc.company_name as companyName,\n" +
        "  CONCAT(DATE(br.period_begin_date), '~', DATE(br.period_end_date)) as periodDescription,\n" +
        "  br.* \n" +
        "FROM\n" +
        "  base_reconciliation br\n" +
        "  INNER JOIN base_company bc ON (bc.id = br.company_id AND bc.del_flag = 0)\n" +
        "  INNER JOIN base_reconciliation_detail brd ON brd.reconciliation_id = br.id \n" +
        "${ew.customSqlSegment}" +
        "</script>")
    Page<BaseReconciliationVo> getPagedReconciliationList(Page page, @Param(Constants.WRAPPER)QueryWrapper queryWrapper);
}

